package com.atguigu.yygh.hosp.receiver;

import com.atguigu.yygh.hosp.repository.ScheduleRepository;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.rabbit.RabbitService;
import com.atguigu.yygh.rabbit.constant.MqConst;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.order.OrderMqVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.Optional;


@Component
public class HospitalReceiver {

    @Autowired
    ScheduleRepository scheduleRepository;

    @Autowired
    ScheduleService scheduleService;

    @Autowired
    RabbitService rabbitService;
    /**
     * 提交订单
     * 监听前先创建交换机和队列，绑定两者
     * @param message
     * @param channel
     * @param orderMqVo
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER,durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER),
            key = {MqConst.ROUTING_ORDER}
    ))
    public void changeScheduleAfterGeneratedOrder(OrderMqVo orderMqVo, Message message, Channel channel) {

        // 1.更新mongodb的availableNumber，这个number是医院系统传进来的，已经-1过了，直接
        Schedule schedule = scheduleRepository.findById(orderMqVo.getScheduleId()).get();
        schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
        schedule.setReservedNumber(orderMqVo.getReservedNumber());  // 医生可挂号数是固定的，maybe 不更新
        scheduleService.updateSchedule(schedule);

        // 2.用MQ调用发送短信服务
        MsmVo msmVo = orderMqVo.getMsmVo();
        Assert.isTrue(msmVo != null, "msmVo短信内容为空");
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM,MqConst.ROUTING_MSM_ITEM,msmVo);

    }


    /**
     * 取消订单,availableNumber+1
     * 监听前先创建交换机和队列，绑定两者
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER_CANCEL,durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER_CANSEL),
            key = {MqConst.ROUTING_ORDER_CANSEL}
    ))
    public void changeScheduleWithCancelOrder(String scheduleId, Message message, Channel channel) {

        // 更新mongodb 的schedule 的availableNumber+1
        Optional<Schedule> optional = scheduleRepository.findById(scheduleId);
        Assert.isTrue(optional.isPresent(),"scheduleId不存在，没有这个排班信息");
        Schedule schedule = optional.get();

        schedule.setAvailableNumber(schedule.getAvailableNumber()+1);
        schedule.setUpdateTime(new Date());
        scheduleRepository.save(schedule);


    }

}
